package com.hengpeng.itfin.persist.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hengpeng.common.persist.AbstractDao;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.itfin.persist.SmsLogDao;
import com.hengpeng.itfinbase.persist.SmsLog;
import com.hengpeng.itfinbase.persist.SmsLog.SmsLogState;
import com.hengpeng.itfinbase.persist.SmsLog.SmsLogTransType;

/**
 * <pre>
 * TODO 短信日志持久层实现
 * </pre>
 * @author pengfangliang
 * @version 1.0, 2010-9-3
 */
@Repository
public class SmsLogDaoImpl extends AbstractDao<SmsLog> implements SmsLogDao
{

	@Override
	public SmsLog insert(SmsLog smsLog)
	{
		super.save(smsLog);
		return smsLog;
	}

	@Override
	public int resetSmsForSending()
	{
		return this.exeNamedQuery("sms.resetForSending");
	}

	@Override
	public Long selectCountSmsLog(String content, String mobile, Integer retryCount, Date sendTime1, Date sendTime2,
			SmsLogState state, String storeId, Date successTime1, Date successTime2, SmsLogTransType type, String userId)
	{
		DynamicQuery query = new DynamicQuery(SmsLog.class);
		if (content != null && content.trim().length() > 0)
			query.like("content", content);
		if (mobile != null && mobile.trim().length() > 0)
			query.like("mobile", mobile);
		if (retryCount != null && retryCount > 0)
			query.eq("retryCount", retryCount);
		if (sendTime1 != null && sendTime2 != null && sendTime1.toString().trim().length() > 0
				&& sendTime2.toString().trim().length() > 0)
			query.between("sendTime", sendTime1, sendTime2);
		if (state != null && state.getText().trim().length() > 0)
			query.eq("state", state);
		if (storeId != null && storeId.trim().length() > 0)
			query.eq("storeId", storeId);
		if (successTime1 != null && successTime2 != null && successTime1.toString().trim().length() > 0
				&& successTime1.toString().trim().length() > 0)
			query.between("successTime", successTime1, successTime2);
		if (type != null && type.getText().trim().length() > 0)
			query.eq("type", type);
		if (userId != null && userId.trim().length() > 0)
			query.eq("userId", userId);
		query.addProjection(Projections.count());
		return super.countByDynamicQuery(query);
	}

	@Override
	public List<SmsLog> selectSmsLogPage(String content, String mobile, Integer retryCount, Date sendTime1,
			Date sendTime2, SmsLogState state, String storeId, Date successTime1, Date successTime2,
			SmsLogTransType type, String userId, int pageIndex, int pageSize)
	{
		DynamicQuery query = new DynamicQuery(SmsLog.class);
		if (content != null && content.trim().length() > 0)
			query.like("content", content);
		if (mobile != null && mobile.trim().length() > 0)
			query.like("mobile", mobile);
		if (retryCount != null && retryCount > 0)
			query.eq("retryCount", retryCount);
		if (sendTime1 != null && sendTime2 != null && sendTime1.toString().trim().length() > 0
				&& sendTime2.toString().trim().length() > 0)
			query.between("sendTime", sendTime1, sendTime2);
		if (state != null && state.getText().trim().length() > 0)
			query.eq("state", state);
		if (storeId != null && storeId.trim().length() > 0)
			query.eq("storeId", storeId);
		if (successTime1 != null && successTime2 != null && successTime1.toString().trim().length() > 0
				&& successTime1.toString().trim().length() > 0)
			query.between("successTime", successTime1, successTime2);
		if (type != null && type.getText().trim().length() > 0)
			query.eq("type", type);
		if (userId != null && userId.trim().length() > 0)
			query.eq("userId", userId);
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		return super.findAllByDynamicQuery(query);
	}

}
